package com.fu.plugin.forms;

import com.fu.plugin.trees.MyTreeStructure;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.treeStructure.SimpleTreeBuilder;

import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;

public class NewForm extends SimpleToolWindowPanel {

    private JPanel parentPanel;

    private JTree tree1;

    public NewForm(Project project) {
        super(true, true);
        add(parentPanel);

        MyTreeStructure structure = new MyTreeStructure(project);
        SimpleTreeBuilder builder = new SimpleTreeBuilder(tree1, (DefaultTreeModel)tree1.getModel(), structure, null);
        Disposer.register(project, builder);
        builder.initRoot();
        builder.expand(structure.getRootElement(), null);

        tree1.addMouseListener(new PopupHandler() {
            @Override
            public void invokePopup(Component component, int x, int y) {
                ActionManager manager = ActionManager.getInstance();
                ActionGroup actionGroup = (ActionGroup)manager.getAction("popupMenu");
                JPopupMenu newMyPopup = manager.createActionPopupMenu("NewMyPopup", actionGroup).getComponent();
                newMyPopup.show(component, x, y);
            }
        });
    }
}
